package mvc.model.dao.impl;

import mvc.common.BaseDao;
import mvc.model.dao.CartDao;
import mvc.model.pojos.CartBean;
import mvc.model.vo.Cart;

import java.util.List;

public class CartDaoImpl implements CartDao {
    private static final BaseDao base;
    static {
        base = new BaseDao();
    }
    @Override
    public CartBean queryOne(int cartnumber) {
        String sql = "select * from cart where cartnumber=?;";
        List<CartBean> query = base.query(sql, CartBean.class, cartnumber);
        return query.isEmpty() ? null : query.get(0);
    }

    @Override
    public List<CartBean> query(int uid) {
        String sql = "select * from cart where uid=? flag=1 ;";
        return base.query(sql, CartBean.class, uid);
    }

    @Override
    public Cart queryOneVO(int cartnumber) {
        String sql = "SELECT cart.cartnumber,p.pname,pi.online,c.color,r.rsize,r.msize,p.price,cart.count,(cart.count*p.price) sum,cart.checked FROM cart,product p,rom r,color c,p_info pi,cpu where cart.pid = p.pid and p.number = pi.number and pi.cid = c.cid and cpu.cpuid = pi.cpuid and pi.rid = r.rid and cart.cartnumber=? cart.flag=1  order by cart.lasttime desc;";
        List<Cart> query = base.query(sql, Cart.class, cartnumber);
        return query.isEmpty() ? null : query.get(0);
    }

    @Override
    public List<Cart> queryVO(int uid) {
        String sql = "SELECT cart.cartnumber,p.pname,pi.online,c.color,r.rsize,r.msize,p.price,cart.count,(cart.count*p.price) sum,cart.checked FROM cart,product p,rom r,color c,p_info pi,cpu where cart.pid = p.pid and p.number = pi.number and pi.cid = c.cid and cpu.cpuid = pi.cpuid and pi.rid = r.rid and cart.uid=? and cart.flag=1 order by cart.lasttime desc;";
        return base.query(sql, Cart.class, uid);
    }

    @Override
    public void upCart(CartBean cartBean, int cartnumber) {
        String sql = "update cart set pid=?,count=?,flag=?,checked = ? where cartnumber=?;";
        base.update(sql, cartBean.getPid(), cartBean.getCount(), cartBean.getFlag(), cartBean.getChecked(), cartnumber);
    }

    @Override
    public void upCartAll(int uid, int checked) {
        String sql = "update cart set checked = ? where uid = ? and cart.flag = 1;";
        base.update(sql, checked, uid);
    }

    @Override
    public List<Cart> queryChecked(int uid) {
        //language=MySQL
        String sql = "SELECT cart.cartnumber,p.pname,pi.online,c.color,r.rsize,r.msize,p.price,cart.count,(cart.count*p.price) sum,cart.checked FROM cart,product p,rom r,color c,p_info pi,cpu where cart.pid = p.pid and p.number = pi.number and pi.cid = c.cid and cpu.cpuid = pi.cpuid and pi.rid = r.rid and cart.uid=? and cart.checked=1 and cart.flag=1 order by cart.lasttime desc;";
        return base.query(sql, Cart.class, uid);
    }

    @Override
    public void addCart(CartBean cart) {
        String sql = "INSERT INTO cart(`pid`, `count`, `uid`, `checked`) VALUES (?, ?, ?, ?);";
        base.update(sql, cart.getPid(), cart.getCount(), cart.getUid(), cart.getChecked());
    }

    @Override
    public CartBean queryByPid(int pid, int uid) {
        String sql = "select * from `cart` where pid = ? and uid = ? and flag=1";
        List<CartBean> query = base.query(sql, CartBean.class, pid, uid);
        return query.isEmpty() ? null : query.get(0);
    }
}
